/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package gui.tools.models;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import lib.exceptions.OverlappedIntervalException;
import lib.model.Interval;
import lib.model.StationCalendar;

/**
 * This class is used as a model to show intervals
 * It shows the most relevant properties of the interval as the end date, and the start date
 * It allows to add and remove intervals from the model, updating the view of the table
 * @see Interval
 * @see JTable
 * @see TableModel
 * @see DefaultTableModel
 * @author Jorge Mira Yagüe
 */
public class TMInterval extends DefaultTableModel {

    private StationCalendar intervals;

    /**
     * Class builder
     * @param intervals The interval list to show in the table
     * @throws OverlappedIntervalException
     */
    public TMInterval(List<Interval> intervals)
            throws OverlappedIntervalException {
        super();

        columnIdentifiers.add("Start date");
        columnIdentifiers.add("End date");
        setIntervals(intervals);
    }

    public List<Interval> getIntervals() {
        return intervals.getIntervals();
    }

    public void removeInterval(int index) {
        intervals.removeInterval(index);
    }

    public Interval getInterval(int index) {
        return intervals.getIntervals().get(index);
    }

    public void setIntervals(List<Interval> intervals)
            throws OverlappedIntervalException {
        this.intervals = new StationCalendar();
        this.intervals.addIntervals(intervals);
        refresh();
    }

    public void addIntervals(List<Interval> intervals)
            throws OverlappedIntervalException {
        this.intervals.addIntervals(intervals);
        refresh();
    }

    public void addInterval(Interval interval)
            throws OverlappedIntervalException {
        this.intervals.addInterval(interval);
        refresh();
    }

    private void refresh() {
        dataVector = new Vector();
        Vector<String> v;
        DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy-HH:mm");
        List<Interval> interv = intervals.getIntervals();
        for (Interval i : interv) {
            v = new Vector();
            v.add(formatter.format(i.getStart().getTime()));
            v.add(formatter.format(i.getEnd().getTime()));
            dataVector.add(v);
        }
    }

    /**
     * This function makes the cells not editables by the user
     * @param x
     * @param y
     * @return always false
     */
    @Override
    public boolean isCellEditable(int x, int y) {
        return false;
    }
}
